<?php
/**
 * Manage user offers.
 *
 * @package    letsManager
 * @subpackage letsUserOffer
 * @author     Jerome Fouilloy
 * @version    SVN: $Rev$
 */
class letsUserOfferActions extends sfActions
{
  /**
   * Form new user offer
   */
  public function executeNew(sfWebRequest $request)
  {
    $this->linkCancel = $this->getUser()->getCurrentReferer();
    $this->member_id = $this->getUserIdModified($request);
    $canAddAskOrOffer = LetsManagerRights::canAddAskOrOffer($this->member_id,$this->getUser()->getAssociationId());
    if(!$canAddAskOrOffer)
    {
      $this->getUser()->setFlash('notice',"Vous avez atteint la limite d'offres et demandes");
      $this->redirect($this->linkCancel);
    }
    $this->form = new LetsUserOfferForm(null,array(
			'context'=> $this->getContext()
    ));
  }
  /**
   * Creating new user offer
   */
  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new LetsUserOfferForm(null,array(
		'context'=> $this->getContext()
    ));
    $this->processForm($request, $this->form, new LetsUserOffer());
    $this->linkCancel = $this->getUser()->getCurrentReferer();
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $id = $request->getParameter('id');
    $this->forward404Unless($userOffer = LetsUserOfferTable::getInstance()->find($id));
    if($userOffer->getMemberId() != $this->getUser()->getUserId() &&
    !$this->hasRightManageOffers() )
    {
      $this->redirect('@error_credentials');
    }
    $this->form = new LetsUserOfferForm($userOffer,array(
			'context'=> $this->getContext()
    ));
    $this->linkCancel = $this->getUser()->getCurrentReferer();
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $id = $request->getParameter('id');
    $this->forward404Unless($userOffer = LetsUserOfferTable::getInstance()->find($id));
    $this->form = new LetsUserOfferForm($userOffer,array(
      'context'=> $this->getContext()
    ));
    	
    $this->processForm($request, $this->form, $userOffer);
    $this->linkCancel = $this->getUser()->getCurrentReferer();
    $this->setTemplate('edit');
  }

  public function executeIndex(sfWebRequest $request)
  {
    $asso_id = $this->getUser()->getAssociationId();
    $page = $request->getParameter('page', 1);
    $this->keySearch = $request->getParameter('query',null);
    $this->category_id = $request->getParameter('category_id', -1);

    $this->offersPager = LetsUserOfferTable::getOffersPager($asso_id,$this->category_id,$page,$this->keySearch);
    $this->searchForm = new LetsCategoryFilterForm(null,array('category_id'=>$this->category_id));
    if($this->keySearch != null)
    {
      $this->searchForm->setDefault('query',$this->keySearch);
    }
    $this->canManageOffers = $this->hasRightManageOffers();

    $this->getUser()->setCurrentReferer('@lets_user_offers_list');
  }

  public function executeDelete(sfWebRequest $request)
  {
    // 	$request->checkCSRFProtection();
    $id = $request->getParameter('id');
    $userOffer = LetsUserOfferTable::getInstance()->find($id);
    $this->forward404Unless($userOffer);
    // suppression d'une demande d'un autre user sans droit manager on refuse
    if($userOffer->getMemberId() != $this->getUser()->getUserId() &&
    !$this->hasRightManageOffers() )
    {
      $this->redirect('@error_credentials');
    }
    else{
      $userOffer->delete();
      $this->redirect($this->getUser()->getCurrentReferer());
    }

  }

  protected function hasRightManageOffers()
  {
    return $this->getUser()->hasCredential(LetsManagerRights::MANAGE_ASKS_OFFERS_OTHERS);
  }

  /**
   * Extract user id from request (if current user edit member)
   * or from the session if not manager
   */
  protected function getUserIdModified(sfWebRequest $request)
  {
    $idReturned = -1;

    if( $this->hasRightManageOffers() &&
    $request->getParameter('member_id',-1) > 0)
    {
      $idReturned = $request->getParameter('member_id');
    }
    else{
      $idReturned = $this->getUser()->getUserId();
    }
    return $idReturned;
  }
  protected function processForm(sfWebRequest $request, sfForm $form,LetsUserOffer  $userOffer)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $values = $form->getValues();

      $userOffer->setName($values['name']);
      $userOffer->setCategoryId($values['category_id']);
      if($userOffer->isNew())
      {
        $userOffer->setMemberId($this->getUserIdModified($request));
      }
      // expire auto au bout de 1 an mais avec confirmation du membre que la demande n'est plus valable
      /*  if($userOffer->getPeriodExpiration() == LetsUserAskTable::DELAY_EXPIRATION_NEVER)
       {
       $userOffer->setAskBeforeDelete(true);
       $nextyear  = mktime(0, 0, 1,date("m"),date("d"),date("Y")+1);
       $userOffer->setDateExpiration(date('Y-m-d',$nextyear));
       }
       else{
       $userOffer->setAskBeforeDelete(false);
       $expiration  = mktime(0, 0, 1,date("m"),date("d") + $userAsk->getPeriodExpiration(),date("Y"));
       $userOffer->setDateExpiration(date('Y-m-d',$expiration));
       }*/
      $userOffer->save();
      $this->redirect($this->getUser()->getCurrentReferer());
    }
  }
}